package com.winit.openapi.apiadapter.controller.validator;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.ValidateUtil;

public class WarehouseInventoryValidator {

    /**
     * 验证查询海外仓总库存入参
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    public static void validateQueryWarehouseStorage(JSONObject json) {
        if (json.containsKey("pageSize") && json.containsKey("pageNum")) {
            ValidateUtil.validInteger(json, "pageSize", true);
            ValidateUtil.validInteger(json, "pageNum", true);
        }

        // 仓库ID必填
        ValidateUtil.validNotNull(json, "warehouseID");
        ValidateUtil.validMaxLength(json, "warehouseID", 10, true);
        ValidateUtil.validInteger(json, "warehouseID", true);

        ValidateUtil.validByRegex(json, "isActive", "(Y|N)", false);

    }

    /**
     * 验证查询查询海外仓产品库存列表参数
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    public static void validateProductInventoryList(JSONObject json) {
        if (json.containsKey("pageSize") && json.containsKey("pageNum")) {
            ValidateUtil.validInteger(json, "pageSize", true);
            ValidateUtil.validInteger(json, "pageNum", true);
        }
        // 产品编码
        ValidateUtil.valid(json, "productCode", false, false, 510);
        // 产品名称
        ValidateUtil.valid(json, "name", false, false, 255);
        // 仓库ID
        if (json.containsKey("warehouseId") && StringUtils.isNotBlank(json.getString("warehouseId"))) {
            String[] warehouseIds = json.getString("warehouseId").split("\\,");
            for (String warehouseId : warehouseIds) {
                if (StringUtils.isBlank(warehouseId)) {
                    throw new ApiException(ErrorCode.FIELD_NOT_NULL, "warehouseId");
                } else if (StringUtils.isNotBlank(warehouseId) && warehouseId.length() >= 10) {
                    throw new ApiException(ErrorCode.FIELD_LENGTH_TO_LONG, "warehouseId", 10);
                }
                boolean result = ValidateUtil.isIntNumber(warehouseId);
                if (!result) {

                    throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, warehouseId);
                }
            }
        }
    }

    /**
     * 验证查询查询海外仓产品库存详情入参
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    public static void validateProductInventoryDatail(JSONObject json) {

        if (json.containsKey("productId")) {
            ValidateUtil.validInteger(json, "productId", false);
        }

        if (json.containsKey("warehouseId") && StringUtils.isNotBlank(json.getString("warehouseId"))) {
            // 仓库ID
            String[] warehouseIds = json.getString("warehouseId").split("\\,");
            for (String warehouseId : warehouseIds) {
                if (StringUtils.isBlank(warehouseId)) {
                    throw new ApiException(ErrorCode.FIELD_NOT_NULL, "warehouseId");
                } else if (StringUtils.isNotBlank(warehouseId) && warehouseId.length() >= 10) {
                    throw new ApiException(ErrorCode.FIELD_LENGTH_TO_LONG, "warehouseId", 10);
                }
                boolean result = ValidateUtil.isIntNumber(warehouseId);
                if (!result) {

                    throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, warehouseId);
                }
            }
        }
    }
}
